﻿using SIMS.Shared.Models;
using SIMS.SqlSugarWebApi.Models;
using SIMS.SqlSugarWebApi.ServiceInterface;

namespace SIMS.SqlSugarWebApi.Services
{
    public class ClassService : BaseService<Class>, IClassService
    {
        private DataContext _context;

        public ClassService(DataContext context) : base(context)
        {
            _context = context;
        }

        public async Task<PagingModel<Class>> GetClassess(string dept, string grade, int pageNum, int pageSize)
        {
            var classes = _context.db.Queryable<Class>()
            .LeftJoin<Student>((c, s) => c.Monitor == s.Id)
            .Select((c, s) => new Class
            {
                Id = c.Id,
                Dept = c.Dept,
                Grade = c.Grade,
                Name = c.Name,
                HeadTeacher = c.HeadTeacher,
                Monitor = c.Monitor,
                MonitorName = s.Name,
                CreateTime = c.CreateTime,
                CreateUser = c.CreateUser,
                LastEditTime = c.LastEditTime,
                LastEditUser = c.LastEditUser
            }).OrderBy((c) => c.Id);

            var items = await classes.ToListAsync();

            return new PagingModel<Class>()
            {

                Items = items
            };
        }
    }
}
